{extend name="index/public" /}

{block name="content"}
<form class="layui-form" action="" id="searchBox">
    <!--<div class="layui-inline">-->
        <!--<input type="text" name="my_user_id" value="" id="my_user_id" placeholder="用户ID/登录名称" autocomplete="off" class="layui-input">-->
    <!--</div>-->
    <!--<div class="layui-inline">-->
        <!--<input type="text" name="role_id" value="" id="role_id" placeholder="角色ID/角色名称" autocomplete="off" class="layui-input">-->
    <!--</div>-->
    <!--<div class="layui-inline">-->
        <!--<input type="text" name="server_id" value="" id="server_id" placeholder="区服ID/区服名称" autocomplete="off" class="layui-input">-->
    <!--</div>-->
    <div class="layui-inline">
        <input type="text" name="my_order_id" value="" id="my_order_id" placeholder="米娱订单号" autocomplete="off" class="layui-input">
    </div>
    <div class="layui-inline">
        <input type="text" name="date" value="{$date}" id="date" placeholder="下单时间" autocomplete="off" class="layui-input">
    </div>
    <!--<div class="layui-inline">-->
        <!--<div class="layui-input-inline">-->
            <!--{MyTag:channel type='manage' /}-->
        <!--</div>-->
    <!--</div>-->
    <!--<div class="layui-inline">-->
        <!--<div class="layui-input-inline">-->
            <!--{MyTag:game type='manage' /}-->
        <!--</div>-->
    <!--</div>-->
    <div class="layui-inline">
        <a href="javascript:;" class="layui-btn searchBtn my-btn my-btn-sm">搜索</a>
    </div>
</form>

<table id="tableBox" lay-filter="tableBox"></table>

<script type="text/html" id="tableBar">
    <a class="layui-btn layui-btn-primary layui-btn-sm" lay-event="view">查看</a>
    {{#  if(d.toberebate_gamecoin > 0){ }}
    <a class="layui-btn layui-btn-green layui-btn-sm" lay-event="rebate">返利</a>
    {{#  } else { }}
    <a class="layui-btn layui-btn-disabled layui-btn-sm" >返利</a>
    {{#  } }}
</script>

{/block}

{block name="css"}
{/block}

{block name="js"}
<script>
    layui.use(['table', 'laydate'], function(){
        var table = layui.table
            ,$ = layui.$
            ,form = layui.form
            ,laydate = layui.laydate
            ,load = null
            ,active = {
                reload: function () {
                    var jsonObj = {
                        "date": $('#date').val()
                        ,"my_user_id": $('#my_user_id').val()
                        ,"role_id": $('#role_id').val()
                        ,"server_id": $('#server_id').val()
                        ,"my_order_id": $('#my_order_id').val()
                        ,"game_id": $('#game_id').val()
                        ,"channel_id": $('#channel_id').val()
                    };
                    table.reload('tableId', {
                        page: {curr: 1}
                        ,where: jsonObj
                        ,height: 'full-100'
                    })
                },
            };

        //日期
        laydate.render({
            elem: '#date'
            ,type: 'date'
            ,range: true
            ,calendar: true
            ,min: -{$dateRange}
            ,max: 0
            ,done: function(value, date, endDate){
                load = layer.load();
                active.reload(value);
                return false;
            }
        });

        //表格初始化
        table.render({
            elem: '#tableBox'
            ,height: 'full-100'
            ,url: '{:url("RebateApply/index")}'
            ,page: true
            ,limit:20
            ,limits:[20,40,60,80]
            ,loading:true
            ,id:'tableId'
            ,done: function (res, curr, count) {
                layer.close(load);
                $('#layui-loading').hide();
            }
            ,cols: [[
                {field:'my_user_name', title: '登录名称', width: 200}
                ,{field:'game_name', title: '游戏名称', width: 200}
                ,{field:'server_name', title: '区服名称', width: 200}
                ,{field:'role_name', title: '角色名称', width: 300}
                ,{field:'date', title: '返利时间', width: 165}
                ,{field:'toberebate_gamecoin', title: '待返游戏币', width: 120}
                ,{field:'other', title: '操作', align:'center', toolbar:'#tableBar'}
            ]]
        });

        $('#searchBox .searchBtn').on('click', function(){
            load = layer.load();
            active['reload'].call(this);
            return false;
        });

        table.on('tool(tableBox)', function(obj){
            var data = obj.data
                datas = obj.data.datas;

            if(obj.event === 'rebate'){
                load = layer.load();
                $.post('{:url("RebateApply/rebate")}',{'game_id':data.game_id,'server_id':data.server_id,'role_id':data.role_id,'my_user_id':data.my_user_id,'date':data.date}, function(json){
                    layer.close(load);
                    if(json.code==0){
                        $(obj.tr).find("td[data-field='other']").find("a[lay-event='rebate']").addClass('layui-btn-disabled').removeClass('layui-btn-green').removeAttr('lay-event');
                        layer.msg(json.msg, {icon: 1, time:1000})
                    }else{
                        layer.msg(json.msg, {icon: 0, time:1000});
                    }
                }, 'JSON').error(function(){
                    layer.msg('连接超时，请联系后台管理员', {icon: 2, time:3000});
                });
            }else if(obj.event === 'view'){
                layer.open({
                    type: 2
                    ,offset: '50px'
                    ,title: '查看'
                    ,area: ['1400px', '700px']
                    ,content: '{:url("RebateApply/view")}'
                    ,success: function(layero, index){
                        var body = layer.getChildFrame('body', index);
                        var html = '<div class="rebateView">' +
                            '<ul class="rebateView-countsum">' +
                                '<li>可返利：<span>'+datas.count_sum.rebateable_gamecoin+'</span></li>' +
                                '<li>已返利：<span>'+datas.count_sum.rebated_gamecoin+'</span></li>' +
                                '<li>待返利：<span>'+datas.count_sum.toberebate_gamecoin+'</span></li>' +
                            '</ul>' +
                            '<div class="rebateView-rule">' +
                                '<div class="rebateView-rule-wrap">' +
                                    '<table class="layui-table" lay-size="sm" cellspacing="0" width="100%">' +
                                        '<thead>' +
                                            '<tr>' +
                                                '<th>订单号</th>' +
                                                '<th>登录名称</th>' +
                                                '<th>渠道名称</th>' +
                                                '<th>游戏名称</th>' +
                                                '<th>区服名称</th>' +
                                                '<th>角色名称</th>' +
                                                '<th>金额</th>' +
                                                '<th>比率</th>' +
                                                '<th>游戏币</th>' +
                                                '<th>支付时间</th>' +
                                                '<th>规则</th>' +
                                                '<th>返利游戏币</th>' +
                                            '</tr>' +
                                        '</thead>' +
                                        '<tbody>';

                                        $i = 0;
                                        for(key in datas) {
                                            if (key !== 'count_sum') {
                                                for(key2 in datas[key]){
                                                    if(key2 !== 'sum'){
                                                        html += '<tr>' +
                                                            '<td>' + datas[key][key2].my_order_id + '</td>' +
                                                            '<td>' + datas[key][key2].my_user_name + '</td>' +
                                                            '<td>' + datas[key][key2].channel_name + '</td>' +
                                                            '<td>' + datas[key][key2].game_name + '</td>' +
                                                            '<td>' + datas[key][key2].server_name + '</td>' +
                                                            '<td>' + datas[key][key2].role_name + '</td>' +
                                                            '<td>' + datas[key][key2].total_price + '</td>' +
                                                            '<td>' + datas[key][key2].ratio + '</td>' +
                                                            '<td>' + datas[key][key2].game_coin + '</td>' +
                                                            '<td>' + datas[key][key2].pay_time + '</td>' +
                                                            '<td>' + datas[key][key2].rule + '</td>' +
                                                            '<td>' + datas[key][key2].rebate_gamecoin + '</td>' +
                                                        '</tr>';
                                                    }else{
                                                        html += '<tr>' +
                                                            '<td colspan="6" class="rebateView-weight">充值金额：'+datas[key][key2].total_amount+'</td>' +
                                                            '<td colspan="6" class="rebateView-weight">可返游戏币：'+datas[key][key2].rebateable_gamecoin+'</td>' +
                                                        '</tr>';
                                                    }
                                                }
                                            }
                                        }
                                    html += '</table>' +
                                '</div>' +
                            '</div>' +
                        '</div>'
                        body.html(html);
                    }
                });
            }
        });
    });
</script>
{/block}